import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:gluz/app/common/resource_picker_sheet.dart';
import 'package:gluz/app/utils/toast_util.dart';

/// 资源选择器使用示例
class ResourcePickerExample extends StatelessWidget {
  const ResourcePickerExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('资源选择器示例'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 基础用法
            ElevatedButton(
              onPressed: () => _showBasicPicker(),
              child: Text('基础资源选择'),
            ),
            
            SizedBox(height: 20),
            
            // 仅图片选择
            ElevatedButton(
              onPressed: () => _showImageOnlyPicker(),
              child: Text('仅图片选择'),
            ),
            
            SizedBox(height: 20),
            
            // 多选模式
            ElevatedButton(
              onPressed: () => _showMultiplePicker(),
              child: Text('多选模式'),
            ),
          ],
        ),
      ),
    );
  }

  /// 基础资源选择
  void _showBasicPicker() {
    ResourcePickerSheet.show(
      onResult: (result) {
        if (result.isSuccess) {
          ToastUtil.show('选择成功: ${result.filePath}');
          print('文件路径: ${result.filePath}');
        } else {
          ToastUtil.show('选择失败: ${result.errorMessage}');
        }
      },
    );
  }

  /// 仅图片选择
  void _showImageOnlyPicker() {
    ResourcePickerSheet.show(
      onResult: (result) {
        if (result.isSuccess) {
          ToastUtil.show('图片选择成功: ${result.filePath}');
          print('图片路径: ${result.filePath}');
        } else {
          ToastUtil.show('图片选择失败: ${result.errorMessage}');
        }
      },
      allowVideo: false, // 不允许视频
      allowMultiple: false,
    );
  }

  /// 多选模式
  void _showMultiplePicker() {
    ResourcePickerSheet.show(
      onResult: (result) {
        if (result.isSuccess) {
          ToastUtil.show('多选成功: ${result.filePath}');
          print('多选文件路径: ${result.filePath}');
        } else {
          ToastUtil.show('多选失败: ${result.errorMessage}');
        }
      },
      allowVideo: true,
      allowMultiple: true, // 允许多选
    );
  }
}

/// 在页面中使用资源选择器的示例
class ExamplePage extends StatelessWidget {
  const ExamplePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('示例页面'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          children: [
            // 头像选择
            _buildSection(
              title: '头像选择',
              subtitle: '点击选择头像图片',
              onTap: () => _selectAvatar(),
            ),
            
            SizedBox(height: 20),
            
            // 文档上传
            _buildSection(
              title: '文档上传',
              subtitle: '选择要上传的文档文件',
              onTap: () => _selectDocument(),
            ),
            
            SizedBox(height: 20),
            
            // 媒体上传
            _buildSection(
              title: '媒体上传',
              subtitle: '选择照片或视频',
              onTap: () => _selectMedia(),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSection({
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.all(16),
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey.shade300),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Row(
          children: [
            Icon(Icons.add_a_photo, color: Colors.blue),
            SizedBox(width: 12),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  SizedBox(height: 4),
                  Text(
                    subtitle,
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.grey.shade600,
                    ),
                  ),
                ],
              ),
            ),
            Icon(Icons.arrow_forward_ios, color: Colors.grey.shade400),
          ],
        ),
      ),
    );
  }

  /// 选择头像
  void _selectAvatar() {
    ResourcePickerSheet.show(
      onResult: (result) {
        if (result.isSuccess) {
          // 处理头像选择
          print('头像路径: ${result.filePath}');
          // 这里可以上传头像到服务器
          ToastUtil.show('头像选择成功');
        } else {
          ToastUtil.show('头像选择失败: ${result.errorMessage}');
        }
      },
      allowVideo: false, // 头像只允许图片
      allowMultiple: false,
    );
  }

  /// 选择文档
  void _selectDocument() {
    ResourcePickerSheet.show(
      onResult: (result) {
        if (result.isSuccess) {
          // 处理文档选择
          print('文档路径: ${result.filePath}');
          // 这里可以上传文档到服务器
          ToastUtil.show('文档选择成功');
        } else {
          ToastUtil.show('文档选择失败: ${result.errorMessage}');
        }
      },
      allowVideo: false, // 文档不需要视频
      allowMultiple: false,
    );
  }

  /// 选择媒体
  void _selectMedia() {
    ResourcePickerSheet.show(
      onResult: (result) {
        if (result.isSuccess) {
          // 处理媒体选择
          print('媒体路径: ${result.filePath}');
          // 这里可以上传媒体到服务器
          ToastUtil.show('媒体选择成功');
        } else {
          ToastUtil.show('媒体选择失败: ${result.errorMessage}');
        }
      },
      allowVideo: true, // 媒体允许视频
      allowMultiple: false,
    );
  }
} 